<?php
/**
 * base controller class for webappkit engine
 * PHP4 version
 */
class WakEngineController {

    /**
     * constructor
     * @param WakEngineResponse $response
     * /
    public function __construct(&$response) {
        $this->response=&$response;
    }/**/

    /**
     * tries to serve response
     * @access public
     * @param WakEngineResponse $response
     */
    function run(&$response) {
        // importing response
        $this->response=&$response;
        // pre-action stuff
        $this->_setUp();
        // searching action to perform
        $method=$response->shiftNode();
        if (!method_exists($this,$method)) {
            // throw new WakEngineErrorPage("controller does not implement requested method ($method)",500);
            return false;
        }
        // echo "<p>$method</p>";
        // performing requested action

        $result=$this->_runActionMethod($method,$response);
        // post-action stuff
        $this->_tearDown();
        return $result;
        //} catch (Exception $e) {
        //return false;
        // }
    }

    /**
     * implement this method to perform pre-action stuff
     * @access protected
     */
    function _setUp() {

    }

    /**
     * implement this method to perform post-action stuff
     */
    function _tearDown() {

    }

    /**
     * performs requested actions
     * @access private
     * @param string $method
     * @param WakEngineResponse $response
     * @return mixed
     */
    function _runActionMethod($method,&$response) {
        // getting remaining nodes
        $nodes=$response->getNextNodes();
        switch (count($nodes)) {
            case 0:
                return $this->$method();
            case 1:
                return $this->$method($nodes[0]);
            case 2:
                return $this->$method($nodes[0],$nodes[1]);
            case 3:
                return $this->$method($nodes[0],$nodes[1],$nodes[2]);
            case 4:
                return $this->$method($nodes[0],$nodes[1],$nodes[2],$nodes[3]);
            default:
                return false;
        }
    }
}
?>